﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OpelWelcomeScreenWeb.NetUtils.Settings;
using System.IO;
using log4net;
using System.Collections;
using System.Collections.Generic;
using OpelWelcomeScreenWeb.Database;
using OpelWelcomeScreenWeb.Models;
using OpelWelcomeScreenWeb.NetUtils;
using Newtonsoft.Json;
using System.Web.Script.Serialization;


namespace OpelWelcomeScreenWeb
{
    public class Services
    {
        protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

       
        /// <summary>
        /// Load Ad Images
        /// </summary>
        /// <param name="path">Ad Images location</param>
        /// <returns></returns>
        public static ArrayList loadAds(string path)
        {
            var dircontent = Directory.GetFiles(path);
            var dl = new ArrayList();
            if (dircontent.Length > 0)
            {
                foreach (string dir in dircontent)
                {
                    // logger.Debug(dir);
                    if (dir.Equals(".") && dir.Equals("..") && (Path.GetExtension(dir).ToLower().Equals("jpg") || Path.GetExtension(dir).ToLower().Equals("gif") ||
                        Path.GetExtension(dir).ToLower().Equals("png") || Path.GetExtension(dir).ToLower().Equals("jpeg")))
                    {
                        dl.Add(dir);
                    }
                }
            }
            dl.Sort();
            return dl;
        }

        /// <summary>
        /// Load all of Work Orders
        /// </summary>
        /// <param name="site">S01</param>
        /// <param name="overdueStr">Over due limit (in minutes): how many minutes before the current time to include the transaction. For example if set 30 min and current time is 10h30 then WO scheduled before 10:00 will not be displayed</param>
        /// <param name="afftertimeStr">After current time limit: how many minutes after current time to include the transactions. For example if 30 is set and current time is 10h30, then WO scheduled after 11h will not be displayed.</param>
        /// <returns>Work Orders</returns>
        public static ArrayList loadWorkOrders(string site, string overdueStr, string aftertimeStr)
        {
            var Settings = AppConfig.GetWebAppSettings();
            var siteID = StringUtils.DefaultIfBlank(site, Settings["DEFAULT_SITE"]);
            int overDue = 0, afterTime = 0;
            if (!Int32.TryParse(overdueStr, out overDue))
                overDue = Int32.Parse(Settings["DEFAULT_OVERDUE"]);

            if (!Int32.TryParse(aftertimeStr, out afterTime))
                afterTime = Int32.Parse(Settings["DEFAULT_AFTERTIME"]);

            var mapWorkOrders = DBAccess.loadWorkOrders(site, overDue, afterTime);

            if (mapWorkOrders.Count > 0)
            {
                //var mapPictures = DBAccess.loadServiceAdvisorPictures(mapWorkOrders.Keys);
                var listResult = new ArrayList();

                foreach (var advisorName in mapWorkOrders.Keys)
                {
                    var sa = new ServiceAdvisor();
                    var listWorks = mapWorkOrders[advisorName] as ArrayList;
                    sa.Name = advisorName as string;
                    sa.WorkOrders = listWorks;
                    if (listWorks !=null && listWorks.Count > 0)
                    {
                        var wo = listWorks[0] as WorkOrder;
                        sa.PictureRef = wo.AdvisorPicRef;   //mapPictures[advisorName] as string;
                    }
                    listResult.Add(sa);
                }

                return listResult;
            }
            else
            {
                return new ArrayList();
            }
        }

    }
}
